package partner;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
 * 一个 bean的 伴生对象
 */
public class BeanObjectPartner {
    private final String tableName;
    List<FieldDecriptor> fieldDecripterList = new ArrayList<FieldDecriptor>();

    public BeanObjectPartner(Object obj) {
        Table table = obj.getClass().getAnnotation(Table.class);
        tableName = table.name();

        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field field : fields) {
            // 该字段不保存
            if(!field.isAnnotationPresent(FieldDes.class)) {
                continue;
            }

            //
            String fieldName = field.getName();
            String fieldClassName = field.getClass().toString();
            // TODO:以后改成使用getter
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(obj);
            } catch(IllegalAccessException e) {
                e.printStackTrace();
            }

            FieldDes fieldDes = field.getAnnotation(FieldDes.class);
            String colName = fieldDes.colName();
            String colDataType = fieldDes.colDataType();
            int colDatalength = fieldDes.colDatalength();
            boolean isPrimaryKey = fieldDes.isPrimaryKey();
            boolean crypt = fieldDes.crypt();

            FieldDecriptor fd = new FieldDecriptor(fieldName,
                                                   colName,
                                                   fieldClassName,
                                                   colDataType,
                                                   colDatalength,
                                                   isPrimaryKey,
                                                   crypt);

            //
            fieldDecripterList.add(fd);
        }
    }

    public String createTableSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("create table if not exists " + tableName + " (");
        for(FieldDecriptor f : fieldDecripterList) {
            //astr varchar(10),
            sb.append(" " + f.colName + " " + f.colDataType);
            if(f.colDatalength > 0) {
                sb.append("(" + f.colDatalength + ")");
            }
            if(f.isPrimaryKey) {
                sb.append(" primary key ");
            }
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(" )");
        return sb.toString();
    }

    public void insert() {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        sb.append("insert into " + tableName + " (");
        for(FieldDecriptor f : fieldDecripterList) {
            sb.append(" " + f.colName + ",");
            sb2.append("?,");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb2.deleteCharAt(sb2.length() - 1);
        sb.append(") values (");
        sb.append(sb2);
        sb.append(")");
        System.out.println("insert:" + sb.toString());


    }

//    private static final class FieldDecriptor {
//        public final boolean isPrimaryKey;//是否主键
//        public final String fieldName;// 名称
//        public final String colName;// 数据库字段名
//        public final String fieldClassName;//数据类型
//        public final String colDataType;//数据库数据类型
//        public final int colDatalength;//未加密时 数据库中长度,仅对 varchar等类型有效
//        public final boolean crypt;//是否加密
//        public final Object fieldValue;//对应的未加密值
//        public Object colValue;//对应的表中的值
//
//        private FieldDecriptor(String fieldName, String colName, String fieldClassName, String colDataType,
//                               int colDatalength, boolean isPrimaryKey, boolean crypt, Object fieldValue) {
//            this.fieldName = fieldName;
//            this.colName = colName;
//            this.fieldClassName = fieldClassName;
//            this.colDataType = colDataType;
//            this.colDatalength = colDatalength;
//            this.isPrimaryKey = isPrimaryKey;
//            this.crypt = crypt;
//            this.fieldValue = fieldValue;
//        }
//    }

    public static void main(String[] args) {

        BeanXyz beanXyz = BeanXyz.genRandomBean();
        BeanObjectPartner partner = new BeanObjectPartner(beanXyz);
        String sqlCreate = partner.createTableSql();
        System.out.println(sqlCreate);

        partner.insert();
    }
}
